package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.PageResult;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mg.TodayBeautyMG;
import com.tanhua.domain.vo.TodayBeautyVo;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mg.ReCommendApi;
import com.tanhua.server.utils.ThreadLocalUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TodayBeautyService {
    //订阅reCommendApi服务接口
    @Reference
    private ReCommendApi reCommendApi;
    //订阅userinfoApi服务接口
    @Reference
    private UserInfoApi userInfoApi;
    //订阅问题服务接口
    @Reference
    private QuestionApi questionApi;

    //注入环信通讯模板
    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //注入TheadLocalUtils方法
    @Autowired
    private ThreadLocalUtils threadUtils;

    /**
     * 推荐今日佳人
     *
     * @return
     */
    public TodayBeautyVo findTodayBeauty() {
        //①从ThreadLocalUtils中获取到userId
        Long userId = threadUtils.getUserId();
        //②调用recommendApi服务接口方法 返回一个recommend对象
        TodayBeautyMG todayBeautyMG = reCommendApi.reCommendById(userId);
        //③判断recommend对象是否为Null 如果为Null 那么创建一个对象 设置默认值id和缘分值
        if (todayBeautyMG == null) {
            todayBeautyMG = new TodayBeautyMG();
            todayBeautyMG.setScore(99.7);
            todayBeautyMG.setUserId(2L);
        }
        return getTodayBeautyVo(todayBeautyMG);
    }

    /**
     * 推荐朋友
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult<TodayBeautyVo> recommendation(Long page, Long pagesize) {
        //1,使用TheadLocalUtils获取到当前的用户id
        Long currentId = threadUtils.getUserId();
        //2,根据用户id,page,pagesize 查询Mongodb中的分页数据 返回一个pageResult<TodayBeautyMG>
        PageResult<TodayBeautyMG> mgPageResult = reCommendApi.reCommendsResult(currentId, page, pagesize);
        List<TodayBeautyMG> todayBeautyMGS = new ArrayList<>();
        //3,判断这个对象是否null
        if (mgPageResult == null || mgPageResult.getItems().size() == 0) {
            //设置---假数据
            mgPageResult = new PageResult<TodayBeautyMG>(10, pagesize.intValue(), 1, page.intValue(), null);
            //4,如果这个对象为null 那么就我们默认给它10个默认的数据
            todayBeautyMGS = defaultTodayBeautyMGS();
        } else {
            //5,如果不为null 调用getItems方法 返回的是MG集合
            todayBeautyMGS = mgPageResult.getItems();
        }
        //5.1 创建一个集合 存储Vo
        List<TodayBeautyVo> todayBeautyVos = new ArrayList<>();
        //6,遍历这个集合
        for (TodayBeautyMG beautyMG : todayBeautyMGS) {
            //存储到vo集合 调用方法 传入beautyMg对象
            todayBeautyVos.add(getTodayBeautyVo(beautyMG));
        }
        //创建一个PageResult<todayBeautyVos>对象
        PageResult<TodayBeautyVo> voPageResult = new PageResult<>();
        //复制对象
        BeanUtils.copyProperties(mgPageResult, voPageResult);
        //重新封装items属性
        voPageResult.setItems(todayBeautyVos);
        return voPageResult;
    }

    /**
     * 默认提供10个数据
     *
     * @return
     */
    private ArrayList<TodayBeautyMG> defaultTodayBeautyMGS() {

        String defaultData = "10043,1,2,3,4,5,6,7,8,9";
        //分割字符串
        String[] split = defaultData.split(",");
        //创建一个集合对象
        ArrayList<TodayBeautyMG> todayBeautyMGS = new ArrayList<>();
        for (String defaultId : split) {
            TodayBeautyMG todayBeautyMG = new TodayBeautyMG();
            todayBeautyMG.setUserId(Long.parseLong(defaultId));
            //随机生成70-90之间的缘分值
            todayBeautyMG.setScore(RandomUtils.nextDouble(70, 90));
            //将生成的对象存入集合
            todayBeautyMGS.add(todayBeautyMG);
        }
        return todayBeautyMGS;
    }

    /**
     * 推荐好友的佳人信息(非缘分值最高)
     */
    public TodayBeautyVo recommendPersonalInfo(Long beautyGirlId) {
        //获取当前id
        Long currentUserId = threadUtils.getUserId();
        //1.根据当前佳人的Id 查询详细信息
        UserInfo userInfo = userInfoApi.findUserInfoByID(beautyGirlId);
        //2.根据传过来的佳人用户Id 和当前用户id 查询缘分值
        TodayBeautyMG todayBeautyMG = reCommendApi.findTodayBeautyMGByTwoId(currentUserId, beautyGirlId);
        //3.将2个对象对应的属性封装到TodaybeautyVo对象 ---参考今日佳人案例
        TodayBeautyVo todayBeautyVo = new TodayBeautyVo();
        BeanUtils.copyProperties(userInfo, todayBeautyVo);
        //判断如果佳人的标签内容不为null 那么就将内容进行拆分注入到vo对象
        if (userInfo.getTags() != null) {
            todayBeautyVo.setTags(userInfo.getTags().split(","));
        }
        //将缘分值封装
        todayBeautyVo.setFateValue(todayBeautyMG.getScore().intValue());
        //因为userInfo的Id是Long类型的 封装不是vo对象 需要手动封装
        todayBeautyVo.setId(userInfo.getId().intValue());
        //返回数据
        return todayBeautyVo;
    }

    /**
     * 查询陌生人的问题
     */
    public String strangerQuestions(Long userId) {
        //查询陌生人问题
        Question question = questionApi.getQuestById(userId.intValue());
        String txt = "花开堪折只需折下一句？";
        //判断对象是否为null
        if (question != null) {
            txt = question.getTxt();
        }
        //返回问题信息
        return txt;
    }

    /**
     * 回复陌生人问题
     */
    public void replyStrangerQuestions(Integer beautyGirlId, String reply) {
        //获取当前用户的id
        Long currentUserId = threadUtils.getUserId();
        //a.根据当前用户Id 获取到当前用户的详细信息
        UserInfo userInfo = userInfoApi.findUserInfoByID(currentUserId);
        //获取到昵称
        String currentUserNickname = userInfo.getNickname();
        //b.根据佳人的ID 查询问题表
        Question question = questionApi.getQuestById(beautyGirlId);
        String txt = "花开堪折只需折下一句？";
        if (question != null){
            txt = question.getTxt();
        }
        //获取佳人问题
        //判断对象是否为null
        //封装消息对象
        Map<String,String> map=new HashMap<String,String>();
        map.put("userId",currentUserId.toString());//发送人
        map.put("nickname",currentUserNickname);//发送人的名称
        map.put("strangerQuestion",txt);//接收者的问题
        map.put("reply",reply);//发送的消息
        //转换成json
        String MessageString = JSON.toJSONString(map);
        //c.调用HuanXinTemplate对象 发送回复内容
        huanXinTemplate.sendMsg(String.valueOf(beautyGirlId),MessageString);
        //注意回复内容需要使用json转换一下
    }

    /**
     * 通过id查询详细详细并封装到对象vo
     * @param todayBeautyMG
     * @return
     */
    private TodayBeautyVo getTodayBeautyVo(TodayBeautyMG todayBeautyMG) {
        //获取今日佳人的id 调用UserInfoApi查询对应的详细信息
        Long uid = todayBeautyMG.getUserId();
        UserInfo userInfo = userInfoApi.findUserInfoByID(uid);
        //创建一个TodayBeantyVo
        //⑤复制userinfo对象到recommendVo对象
        TodayBeautyVo todayBeautyVo = new TodayBeautyVo();
        BeanUtils.copyProperties(userInfo, todayBeautyVo);
        //判断如果佳人的标签内容不为null 那么就将内容进行拆分注入到vo对象
        if (userInfo.getTags() != null) {
            todayBeautyVo.setTags(userInfo.getTags().split(","));
        }
        //将缘分值封装
        todayBeautyVo.setFateValue(todayBeautyMG.getScore().intValue());
        //因为userInfo的Id是Long类型的 封装不是vo对象 需要手动封装
        todayBeautyVo.setId(userInfo.getId().intValue());
        //⑥返回给前端
        return todayBeautyVo;
    }
}
